package com.fengmi.service.impl;

import com.fengmi.dto.PageDTO;
import com.fengmi.entity.MallGoodsBrand;
import com.fengmi.mapper.MallGoodsBrandMapper;
import com.fengmi.service.MallGoodsBrandService;

import com.fengmi.vo.PageResoultVo;
import com.fengmi.vo.ResoultVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;


@Service
public class MallGoodsBrandServiceImpl implements MallGoodsBrandService {

    @Autowired(required = false)
    private MallGoodsBrandMapper mallGoodsBrandMapper;


    @Override
    public PageResoultVo<MallGoodsBrand> selectPageGoodBrand(PageDTO<MallGoodsBrand> pageDTO) {
        //判断传入的前端数据是否为空
        if (pageDTO==null){
         return  new PageResoultVo<>("参数非法",false);
      }
        //类型转换
        Integer page = Integer.parseInt(pageDTO.getPageNum());
        Integer size = Integer.parseInt(pageDTO.getPageSize());
        //非空判断,分页默认值
        if (page==null||page<=0){
            page=1;
        }
        if (size==null||size<=0){
            size=6;
        }
        //分页拦截器实现分页
        PageHelper.startPage(page,size);
        //查找所有信息
        List<MallGoodsBrand> pageGoodBrand = mallGoodsBrandMapper.findPageGoodBrand(pageDTO.getWhere());
        //分页信息对象创建，放入查询集合
        PageInfo<MallGoodsBrand> mallGoodsBrandPageInfo = new PageInfo<>(pageGoodBrand);
        //返回分页信息
        PageResoultVo<MallGoodsBrand> mallGoodsBrandPage = new PageResoultVo<MallGoodsBrand>("查询成功",true);
        //设置分页信息
        mallGoodsBrandPage.setData(mallGoodsBrandPageInfo.getList());
        //设置总页数
        mallGoodsBrandPage.setTotalPage(mallGoodsBrandPageInfo.getPages());
        //设置总记录数
        mallGoodsBrandPage.setTotalSize(mallGoodsBrandPageInfo.getTotal());
        return mallGoodsBrandPage;
    }

    @Override
    public ResoultVo<MallGoodsBrand> findOneBrandById(String id) {
        if (StringUtils.isEmpty(id)){
            return  new ResoultVo(false,"参数非法");
        }
        Integer anInt = Integer.parseInt(id);
        MallGoodsBrand oneBrandById = mallGoodsBrandMapper.getOneBrandById(anInt);

        if (oneBrandById==null){
            return  new ResoultVo(false,"我乏了，出去逛一会儿~~~~");
        }

        return new ResoultVo(true,"操作成功",oneBrandById);
    }


    @Override
    public ResoultVo creatGoodBrand(MallGoodsBrand ma) {
        if (ma==null){
            return  new ResoultVo(false,"参数非法");
        }
        Integer i = mallGoodsBrandMapper.addGoodBrand(ma);
        if (i>0){
            return new ResoultVo<>(true,"操作成功");
        }else {
            return new ResoultVo<>(false,"操作失败");
        }

    }

    @Override
    public ResoultVo changeGoodBrand(MallGoodsBrand goods) {
        if (goods==null){
            return  new ResoultVo(false,"参数非法");
        }
        Integer i = mallGoodsBrandMapper.updateGoodBrand(goods);
        if (i>0){
            return new ResoultVo(true,"操作成功");
        }else {
            return new ResoultVo(false,"操作失败");
        }
    }

    @Override
    public ResoultVo changeBrandStatusById(String id, String status) {
         if (StringUtils.isEmpty(id)||StringUtils.isEmpty(status)){
             return  new ResoultVo(false,"参数非法");
         }
        Integer ID= Integer.parseInt(id);
        Integer Status = Integer.parseInt(status);
        Integer i = mallGoodsBrandMapper.updateBrandStatusById(ID, Status);
        if (i>0){
            return new ResoultVo(true,"操作成功");
        }else {
            return new ResoultVo(false,"操作失败");
        }
    }

    @Override
    public ResoultVo delGoodBrandById(String id) {
        if (StringUtils.isEmpty(id)){
            return  new ResoultVo(false,"参数非法");
        }
        Integer ID = Integer.parseInt(id);
        int i = mallGoodsBrandMapper.delGoodBrandById(ID);
        if (i>0){
            return new ResoultVo(true,"操作成功");
        }else {
            return new ResoultVo(false,"操作失败");
        }
    }

    @Override
    public ResoultVo removeMore(String ids) {
        if (StringUtils.isEmpty(ids)){
            return new ResoultVo(false,"参数不合法");
        }
        String[] split = ids.split(",");
        Integer[] Ids = new Integer[split.length];

        for (int i=0;i<split.length;i++){
            int anInt = Integer.parseInt(split[i]);
            Ids[i]=anInt;
        }

        int i = mallGoodsBrandMapper.delMore(Ids);
        if (i>0){
            return new ResoultVo(true,"操作成功");
        }else {
            return new ResoultVo(false,"操作失败");
        }
    }
}
